package cn.com.zetatech.loader.common.util;

import cn.com.zetatech.loader.common.message.ResolverMessageExtraParameter;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class FilePathFormater {

    static String YEAR = "YYYY";
    static String MONTH = "MM";
    static String DAY = "DD";
    static String HOUR = "HH";
    static String MINUTE = "mm";

    /**
     * @param pathExpression 路径表达式
     * @param staticProps    静态属性
     * @param date           日期
     */
    public static String formatByExtraParameter(String pathExpression, Map<String, ResolverMessageExtraParameter> staticProps, Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            pathExpression = pathExpression.replaceAll("\\{" + YEAR + "}", String.valueOf(calendar.get(Calendar.YEAR)));
            pathExpression = pathExpression.replaceAll("\\{" + MONTH + "}", String.format("%02d", calendar.get(Calendar.MONTH) + 1));
            pathExpression = pathExpression.replaceAll("\\{" + DAY + "}", String.format("%02d",calendar.get(Calendar.DAY_OF_MONTH)));
            pathExpression = pathExpression.replaceAll("\\{" + HOUR + "}", String.format("%02d",calendar.get(Calendar.HOUR_OF_DAY)));
            pathExpression = pathExpression.replaceAll("\\{" + MINUTE + "}", String.format("%02d",calendar.get(Calendar.MINUTE)));
        }
        if (staticProps != null) {
            for (String key : staticProps.keySet()) {
                ResolverMessageExtraParameter resolverMessageExtraParameter = staticProps.get(key);
                pathExpression = pathExpression.replaceAll("\\{" + key + "}", resolverMessageExtraParameter.getPropertyValue());
            }
        }
        return pathExpression;
    }

    public static String format(String pathExpression, Map<String, String> staticProps, Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            pathExpression = pathExpression.replaceAll("\\{" + YEAR + "}", String.valueOf(calendar.get(Calendar.YEAR)));
            pathExpression = pathExpression.replaceAll("\\{" + MONTH + "}", String.format("%02d", calendar.get(Calendar.MONTH) + 1));
            pathExpression = pathExpression.replaceAll("\\{" + DAY + "}", String.format("%02d",calendar.get(Calendar.DAY_OF_MONTH)));
            pathExpression = pathExpression.replaceAll("\\{" + HOUR + "}", String.format("%02d",calendar.get(Calendar.HOUR_OF_DAY)));
            pathExpression = pathExpression.replaceAll("\\{" + MINUTE + "}", String.format("%02d",calendar.get(Calendar.MINUTE)));
        }
        if (staticProps != null) {
            for (String key : staticProps.keySet()) {
                pathExpression = pathExpression.replaceAll("\\{" + key + "}", staticProps.get(key));
            }
        }
        return pathExpression;
    }


    public static void main(String[] args) {
        Date date=new Date(1704042120000L);
        String pathExpression = "/{EQP}/{YYYY}-{MM}-{DD}/{HH}/{custom_wafer_key}";
        Map<String, String> staticProps = new HashMap<>();
        staticProps.put("EQP", "EQP_1");
        staticProps.put("custom_wafer_key", "custom_wafer_key_1");
        System.out.println(format(pathExpression, staticProps, date));

        Map<String, ResolverMessageExtraParameter> staticProps2 = new HashMap<>();

        ResolverMessageExtraParameter resolverMessageExtraParameter = new ResolverMessageExtraParameter();
        resolverMessageExtraParameter.setPropertyName("EQP");
        resolverMessageExtraParameter.setPropertyValue("EQP_1");
        resolverMessageExtraParameter.setType("");
        resolverMessageExtraParameter.setExpression("");
        staticProps2.put("EQP", resolverMessageExtraParameter);

        ResolverMessageExtraParameter resolverMessageExtraParameter2 = new ResolverMessageExtraParameter();
        resolverMessageExtraParameter2.setPropertyName("custom_wafer_key");
        resolverMessageExtraParameter2.setPropertyValue("custom_wafer_key_1");
        resolverMessageExtraParameter2.setType("");
        resolverMessageExtraParameter2.setExpression("");
        staticProps2.put("custom_wafer_key", resolverMessageExtraParameter2);

        System.out.println(formatByExtraParameter(pathExpression, staticProps2, date));
    }
}
